// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/sparepartshospital/api/SpareStockApi'

import ComClass from '@api/ComQuery'
import ListClass from '@api/sparepartshospital/model/SpareStockListQueryReq'
import EditClass from '@api/sparepartshospital/model/SpareStockOperateReq'
import BaseClass from '@api/sparepartshospital/model/SpareStockDto'
import ChildClass from '@api/sparepartshospital/model/SpareDto'
import Child2Class from '@api/sparepartshospital/model/WarehouseDto'

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']
const BASE = [BaseClass, 'content']
const CHILD = [ChildClass, 'spareDto']
const CHILD2 = [Child2Class, 'warehouseDto']

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  validParam(param) {
    return !CORE.isEmpty(param.statusList)
  },
  transParam(param) {
    const warehouseId = param.warehouseId ? [param.warehouseId] : []
    return { ...param, warehouseId }
  },
  async getList(param, TIP = '库存管理列表查询') {
    const DTO = CORE.grpcDTO(ListClass, param, COM, BASE)
    DTO.setStatus(param.statusList)
    const resp = await CORE.grpc(TIP, CLIENT, 'querySpareStockList', DTO)
    return [resp.comQuery.totalCount, resp.content || [], resp.map || {}]
  },
  dealList([obj], { state: { MAP } }) {
    if (MAP.COUNT) MAP.COUNT = obj || {}
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  async edit(one, TIP = '修改') {
    const DTO = new EditClass()
    const baseDTO = CORE.grpcDTO(BaseClass, one, CHILD, CHILD2)
    DTO.setContent(baseDTO)

    await CORE.grpc(TIP, CLIENT, 'updateSpareStock', DTO)
    return CORE.grpcOK(TIP)
  },
}
